﻿using System;


using TrivialPurfuit.Data;

namespace TrivialPurfuit.Business
{
    public class QuestionCategory : BindableObject
    {
        private TrivialPurfuit.Data.TPDataSet.CategoryRow _categoryRow;

        public int ID
        {
            get
            {
                return _categoryRow.id;
            }
        }

        public string Name
        {
            get
            {
                return _categoryRow.name;
            }
            set
            {
                this._categoryRow.name = value;
                this.RaisePropertyChanged("Name");
            }
        }

        public string Color
        {
            get
            {
                return _categoryRow.color;
            }
            set
            {
                this._categoryRow.color = value;
                this.RaisePropertyChanged("Color");
            }
        }

        public void changeWithoutReplacing(QuestionCategory category)
        {
            this._categoryRow = category._categoryRow;

            this.RaisePropertyChanged("Name");
            this.RaisePropertyChanged("Color");
        }

        public QuestionCategory(TrivialPurfuit.Data.TPDataSet.CategoryRow categoryRow)
        {
            this._categoryRow = categoryRow;
        }

        public QuestionCategory()
        {
            this._categoryRow = Data.QuestionData.Instance.Catagory.NewCategoryRow();

            this._categoryRow.id = QuestionData.Instance.getNextID("Category");
            this._categoryRow.name = "";
            this._categoryRow.color = "White";

            Data.QuestionData.Instance.Catagory.AddCategoryRow(this._categoryRow);
        }

        public QuestionCard getRandomCard()
        {
            TPDataSet.QuestionRow[] cards = _categoryRow.GetQuestionRows();
            
            // This will generate a number with 0 the lowest and length - 1 the max.
            int randomIndex = new Random().Next(0, cards.Length);
            
            try
            {
                return new QuestionCard(cards[randomIndex]);
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show("There are no questions in this category.", 
                    "No Questions", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);

                throw new Exception("There are no questions in this category.", e);
            }
        }

    }
}
